<?php

/**
 * make工具控制器
 * @author  ZhouHaoHua <53027327@qq.com>
 * @package
 */
class make_Action extends Abstract_Action {
    /**
     * 项目名称
     * @var string
     */
    private $projectName;
    /**
     * 项目根路径
     * @var string
     */
    private $projectRoot;
    /**
     * 当前项目的具体路径
     * @var string
     */
    private $projectDir;
    /**
     * @var Project
     */
    private $P;

    /**
     * make工具控制器
     */
    public function __construct() {
        $this->projectRoot = realpath(Z_DIR . '/www');
        if($this->projectRoot === false){
            \Z::DirFile()->createDir(Z_DIR . '/www');
            $this->projectRoot = realpath(Z_DIR . '/www');
        }
        parent::__construct();
        $this->projectName = $_GET['project'];
        $this->projectDir  = $this->projectRoot . '/' . $this->projectName;
        $this->P           = new Project($this->projectDir);
    }

    /**
     * 默认动作
     */
    public function index() {
        print_r($_GET);
        $this->data['txt'] = 'Welcome';
        print_r($this->data['txt']);
        exit;
    }

    /**
     * 添加新项目，如果项目已经存在则不添加
     */
    public function addProject() {
        $this->P->add();
        exit();
    }

    /**
     * 重设项目
     */
    public function resetProject() {
        $this->P->del()->add();
        exit();
    }

    /**
     * 删除项目
     */
    public function delProject() {
        $this->P->del();
        exit();
    }

    /**
     * 添加控制器
     */
    public function addCon() {
        if ($_GET[3] && $this->P->isExists()) {
            $this->P->addControllers($_GET[3]);
            if (!isset($_GET[4]) || strtolower($_GET[4]) != 'notview') {
                $this->P->addViews($_GET[3]);
            }
        } else {
            echo "Error:Fail.";
        }
        exit;
    }

    /**
     * 创建前端视图文件
     */
    public function addView() {
        if ($_GET[3] && $this->P->isExists()) {
            $this->P->addViews($_GET[3]);
        } else {
            echo "Error:Fail.";
        }
        exit;
    }

    /**
     * 添加模块
     */
    public function addModel() {
        $table = $_GET[3];
        if ($table && $this->P->isExists()) {
            $this->P->addModels($table);
        } else {
            echo "Error:Fail.";
        }
        exit;
    }

    public function __call($name, $arguments) {
        $txt = "No existing method: '$name' arguments:" . implode(', ', $arguments) . "\n";
        exit($txt);
    }
}